'''
@File    :   ReadDTCInfomation_NRC.py
@Time    :   2025/3/14 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True
import Set_up

#NRC13
ReadDTCInfo_NRC13_phy = [   ([0x19],[0x7F,0x19,0x13]),                       
                            ([0x19,0x01],[0x7F,0x19,0x13]),
                            ([0x19,0x02],[0x7F,0x19,0x13]), 
                            ([0x19,0x04],[0x7F,0x19,0x13]), 
                            ([0x19,0x06],[0x7F,0x19,0x13]), 
                            ([0x19,0x01,0x01,0x02],[0x7F,0x19,0x13]),
                            ([0x19,0x02,0x01,0x02],[0x7F,0x19,0x13]),
                            ([0x19,0x04,0xF0],[0x7F,0x19,0x13]), 
                            ([0x19,0x04,0xF0,0x03],[0x7F,0x19,0x13]), 
                            ([0x19,0x04,0xF0,0x03,0x16],[0x7F,0x19,0x13]), 
                            ([0x19,0x04,0xF0,0x03,0x16,0x01,0x02],[0x7F,0x19,0x13]), 
                            ([0x19,0x06,0xF0],[0x7F,0x19,0x13]), 
                            ([0x19,0x06,0xF0,0x03],[0x7F,0x19,0x13]), 
                            ([0x19,0x06,0xF0,0x03,0x16],[0x7F,0x19,0x13]), 
                            ([0x19,0x06,0xF0,0x03,0x16,0x01,0x02],[0x7F,0x19,0x13]), 
                            ([0x19,0x0A,0x01],[0x7F,0x19,0x13]),
                            ([0x19,0x0A,0x01,0x02],[0x7F,0x19,0x13]),
                            ([0x19,0x0A,0x01,0x02,0x03],[0x7F,0x19,0x13]),
                            ([0x19,0x0A,0x01,0x02,0x03,0x04],[0x7F,0x19,0x13]),
                        ] 

#NRC12
ReadDTCInfo_NRC12_phy = [   ([0x19,0x08,0x01,0x02],[0x7F,0x19,0x12]),            
                            ([0x19,0x03,0x01,0x02,0x03,0x04],[0x7F,0x19,0x12]), 
                            ([0x19,0x05,0x01],[0x7F,0x19,0x12]), 
                            ([0x19,0x07,0x01,0x02],[0x7F,0x19,0x12]),
                            ([0x19,0x09,0x01,0x02,0x03],[0x7F,0x19,0x12]),
                            ([0x19,0x11,0x01,0x02,0x03],[0x7F,0x19,0x12]),
                            ([0x19,0x12,0x01,0x02,0x03],[0x7F,0x19,0x12]),
                            ([0x19,0x13],[0x7F,0x19,0x12]),
                            ([0x19,0x14],[0x7F,0x19,0x12]),
                            ([0x19,0x15],[0x7F,0x19,0x12]),
                            ([0x19,0x16],[0x7F,0x19,0x12]),
                            ([0x19,0x00],[0x7F,0x19,0x12]),
                         ]

# 处理 Set_up.DTC_list 部分
dtc_list_part_04 = [([0x19, 0x04] + list(item)[0] + [0x02], [0x7F, 0x19, 0x31]) for item in Set_up.DTC_list]
dtc_list_part_06 = [([0x19, 0x06] + list(item)[0] + [0x02], [0x7F, 0x19, 0x31]) for item in Set_up.DTC_list]

# NRC31
fixed_part  = [ ([0x19,0x04,0xD0,0xA2,0x87,0x00],[0x7F,0x19,0x31]),
                ([0x19,0x04,0xD0,0x97,0x87,0x03],[0x7F,0x19,0x31]),
                ([0x19,0x04,0xC0,0x7B,0x88,0x04],[0x7F,0x19,0x31]),
                ([0x19,0x04,0xF0,0x03,0x16,0x09],[0x7F,0x19,0x31]),
                ([0x19,0x04,0xF0,0x03,0x17,0xA0],[0x7F,0x19,0x31]),
                ([0x19,0x04,0xD3,0x05,0x55,0xA1],[0x7F,0x19,0x31]),
                ([0x19,0x04,0xD3,0x05,0x56,0xA8],[0x7F,0x19,0x31]),

                ([0x19,0x06,0xD0,0xA2,0x87,0x00],[0x7F,0x19,0x31]),
                ([0x19,0x06,0xD0,0x97,0x87,0x05],[0x7F,0x19,0x31]),
                ([0x19,0x06,0xC0,0x7B,0x88,0x06],[0x7F,0x19,0x31]),
                ([0x19,0x06,0xF0,0x03,0x16,0x09],[0x7F,0x19,0x31]),
                ([0x19,0x06,0xF0,0x03,0x17,0xA0],[0x7F,0x19,0x31]),
                ([0x19,0x06,0xD3,0x05,0x55,0xA1],[0x7F,0x19,0x31]),
                ([0x19,0x06,0xD3,0x05,0x56,0xA8],[0x7F,0x19,0x31]),
            ]

ReadDTCInfo_NRC31_phy = dtc_list_part_04 + dtc_list_part_06 + fixed_part

#NRC13
ReadDTCInfo_NRC12_Priority = [  ([0x19,0x08],[0x7F,0x19,0x12]),            
                                ([0x19,0x03,0x01,0x02],[0x7F,0x19,0x12]), 
                                ([0x19,0x05],[0x7F,0x19,0x12]), 
                                ([0x19,0x07],[0x7F,0x19,0x12]),
                                ([0x19,0x11],[0x7F,0x19,0x12]),
                                ([0x19,0x12],[0x7F,0x19,0x12]),
                                ([0x19,0x13],[0x7F,0x19,0x12])
                            ]

ReadDTCInfo_NRC_no_resp_Fun = [ ([0x19,0x01],[]), 
                                ([0x19,0x02],[]), 
                                ([0x19,0x06],[]),
                                ([0x19,0x05],[]),          
                                ([0x19,0x0A],[]), 
                                ([0x19,0x05],[]), 
                                ([0x19,0x07],[]), 
                                ([0x19,0x04,0x5C,0x1A,0x90,0x01],[]),
                                ([0x19,0x04,0x11,0x22,0x33,0x01],[]), 
                                ([0x19,0x04,0x5C,0x03,0x17,0x00],[])]

#NRC12
ReadDTCInfo_NRC11_phy = [   ([0x19, 0x01, 0x01], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x01, 0x08], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x01, 0x09], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x01, 0x0A], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x01, 0x0B], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x01, 0xFF], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x02, 0x01], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x02, 0x08], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x02, 0x09], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x02, 0x0A], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x02, 0x0B], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x02, 0xFF], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x04, 0xAC,0xD1,0x78,0x01], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x04, 0xAC,0xD1,0x78,0x02], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x06, 0xAC,0xD2,0x78,0x01], [0x7F, 0x19, 0x11]),
                            ([0x19, 0x06, 0xAC,0xD2,0x78,0x02], [0x7F, 0x19, 0x11]),
                         ]

class Test_ReadDTCInfomation_NRC_phy(object):

    '''NRC13'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC13_phy]
    @allure.severity('normal')
    @allure.story('Test NRC13 of read dtc information service 0x19')
    @allure.description('Test NRC13 of read dtc information service 0x19 in default session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_TC000377_default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC13 of read dtc information service 0x19')
    @allure.description('Test NRC13 of read dtc information service 0x19 in extended session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_TC000377_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')



    '''NRC12'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC12_phy]
    @allure.severity('normal')
    @allure.story('Test NRC12 of read dtc information service 0x19')
    @allure.description('Test NRC12 of read dtc information service 0x19 in default session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_TC000376_default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC12 of read dtc information service 0x19')
    @allure.description('Test NRC12 of read dtc information service 0x19 in extended session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_TC000376_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')


    '''NRC31'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC31_phy]
    @allure.severity('normal')
    @allure.story('Test NRC31 of read dtc information service 0x19')
    @allure.description('Test NRC31 of read dtc information service 0x19 in default session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_TC000378_defualt_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC31 of read dtc information service 0x19')
    @allure.description('Test NRC31 of read dtc information service 0x19 in extended session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_TC000378_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')



    '''NRC13_Priority'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC12_Priority]
    @allure.severity('normal')
    @allure.story('Test NRC13 Priority of read dtc information service 0x19')
    @allure.description('Test NRC13 Priority of read dtc information service 0x19 in default session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC13 Priority of read dtc information service 0x19')
    @allure.description('Test NRC13 Priority of read dtc information service 0x19 in extended session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')


    '''NRC7F'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC11_phy]
    @allure.severity('normal')
    @allure.story('Test NRC7F of read dtc information service 0x19')
    @allure.description('Test NRC7F of read dtc information service 0x19 in default session with physical addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC11_phy, ids=ids)
    def test_reportNumberOfDTC_NRC11_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDS', 'noClearFault')

class Test_ReadDTCInfomation_NRC_Fun(object):

    '''NRC13'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC13_phy]
    @allure.severity('normal')
    @allure.story('Test NRC13 of read dtc information service 0x19')
    @allure.description('Test NRC13 of read dtc information service 0x19 in default session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_TC000377_default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC13 of read dtc information service 0x19')
    @allure.description('Test NRC13 of read dtc information service 0x19 in extended session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_TC000377_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')



    '''NRC12'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC12_phy]
    @allure.severity('normal')
    @allure.story('Test NRC12 of read dtc information service 0x19')
    @allure.description('Test NRC12 of read dtc information service 0x19 in default session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_TC000376_default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC12 of read dtc information service 0x19')
    @allure.description('Test NRC12 of read dtc information service 0x19 in extended session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_TC000376_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')


    '''NRC31'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC31_phy]
    @allure.severity('normal')
    @allure.story('Test NRC31 of read dtc information service 0x19')
    @allure.description('Test NRC31 of read dtc information service 0x19 in default session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_TC000378_defualt_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC31 of read dtc information service 0x19')
    @allure.description('Test NRC31 of read dtc information service 0x19 in extended session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_TC000378_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')



    '''NRC13_Priority'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC12_Priority]
    @allure.severity('normal')
    @allure.story('Test NRC13 Priority of read dtc information service 0x19')
    @allure.description('Test NRC13 Priority of read dtc information service 0x19 in default session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test NRC13 Priority of read dtc information service 0x19')
    @allure.description('Test NRC13 Priority of read dtc information service 0x19 in extended session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')


    '''NRC7F'''
    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '), resp) for req, resp in ReadDTCInfo_NRC11_phy]
    @allure.severity('normal')
    @allure.story('Test NRC7F of read dtc information service 0x19')
    @allure.description('Test NRC7F of read dtc information service 0x19 in default session with Functional addressing')
    
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC11_phy, ids=ids)
    def test_reportNumberOfDTC_NRC11_Programming_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun', 'noClearFault')